1
Natural Syntax: The Philosophy of Operator Overloading
AI037 Lesson 17
00:00

The philosophy of Natural Syntax argues that user-defined types should behave as first-class citizens. By implementing operator overloading, we allow classes to use standard notation (like + or ==), which reduces cognitive load and fulfills the principle of least astonishment.

1. Anatomy & Dispatch

An operator is a function with a special name: the keyword operator followed by a symbol. A unary operator has one operand, while a binary operator has two. When defined as a member function, the left-hand operand binds to the implicit this pointer (a.operator+(b)). As a nonmember, both are explicit (operator+(a, b)).

Overloadable + - * / % ^ & | ~ ! = < > += -= *= /= << >> == != <= >= && || ++ -- , ->* -> () [] Non-Overloadable :: (Scope) .* (Member deref) . (Dot) ?: (Conditional) Table 14.1. Operator Permissions (Ref. p. 684)

2. Constraints & Ethics

C++ prevents "language vandalism": you cannot create new symbols (e.g., **) or redefine operations for built-in types (e.g., int + int). Precedence and associativity are immutable. Architecture principle: Classes defining == integrate effortlessly with library algorithms like std::find.

main.py
TERMINAL bash — 80x24
> Ready. Click "Run" to execute.
>